<?php

namespace App\Http\Models\Report;

use App\Http\Models\Business\PredictInvoiceMonth;
use App\Http\Models\Business\PredictInvoiceYear;
use App\Http\Models\Business\CacheInvoiceRegion;

class ReportRegionIndicator
{
    private $year = 0;
    private $month = 0;
    public $regions = null;
    private $predict_invoice_years = null;
    private $predict_invoice_months = null;
    private $cache_invoice_regions = null;

    public $total_predict_month = 0;
    public $total_real_month = 0;
    public $total_predict_year = 0;
    public $total_real_year = 0;


    public function __construct(int $year, int $month)
    {
        $this->year = $year;
        $this->month = $month;

        $this->regions = InvoiceRegion::all();
        //预估开票金额年份
        $this->predict_invoice_years = PredictInvoiceYear::where(['year' => $this->year])->get();
        //预估开票金额月份
        $this->predict_invoice_months = PredictInvoiceMonth::where(['year' => $this->year, 'month' => $this->month])->get();
        //缓存的销售大区开票数据
        $this->cache_invoice_regions = CacheInvoiceRegion::where(['year' => $this->year])->get();

        $this->init();
    }

    private function init()
    {
        //将数据分配给各个大区
        foreach ($this->regions as $region) {
            $region->year = $this->year;
            $region->month = $this->month;

            foreach ($this->predict_invoice_years as $value) {
                if ($region->code == $value->sales_region_code) {
                    $region->predict_invoice_year = $value->amount;
                }
            }

            foreach ($this->predict_invoice_months as $value) {
                if ($region->code == $value->sales_region_code  && $this->month == $value->month) {
                    $region->predict_invoice_month = $value->amount;
                }
            }

            foreach ($this->cache_invoice_regions as $value) {
                if ($region->code == $value->sales_region_code) {
                    $region->cache_invoice_regions_model[] = $value;
                }
            }

            $region->init();
        }

        $this->setTotalPredictMonth();
        $this->setTotalPredictYear();
        $this->setTotalReal();
    }

    public function getTotalProgressMonth()
    {
        $result = 0;
        if ($this->total_predict_month != 0) {
            $result = $this->total_real_month  / $this->total_predict_month;
        }
        return $result;
    }

    public function getTotalProgressYear()
    {
        $result = 0;
        if ($this->total_predict_year != 0) {
            $result = $this->total_real_year  / $this->total_predict_year;
        }
        return $result;
    }

    private function setTotalPredictMonth()
    {
        $result = 0;
        foreach ($this->predict_invoice_months as $value) {
            $result += $value->amount;
        }
        $this->total_predict_month = $result;
    }


    private function setTotalPredictYear()
    {
        $result = 0;
        foreach ($this->predict_invoice_years as $value) {
            $result += $value->amount;
        }
        $this->total_predict_year = $result;
    }


    private function setTotalReal()
    {
        $total_month = 0;
        $total_year = 0;
        foreach ($this->cache_invoice_regions as $value) {
            // dump($value);
            if ($value->month == $this->month) {
                $total_month += $value->amount;
            }
            $total_year += $value->amount;
        }

        $this->total_real_month = $total_month;
        $this->total_real_year = $total_year;
    }
}
